Gerbrand Koren, 8 Feb 2017
This demo illustrates how performance of python code can be improved by using just-in-time (JIT) compilation.
Advantages of JIT:
JIT can be imported from the numba package (which is part of the Anaconda distribution). The speed up that can be achieved by using JIT is different for each code.
In [1]:
# -- IPython settings
%reset -f
%matplotlib inline
# -- Import statement
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
from numba import jit
In [2]:
# -- Test loop
def testloop(i_max):
x = 0
for i in range(i_max):
x = x + i
# -- Test loop with JIT
@jit
def testloop_jit(i_max):
x = 0
for i in range(i_max):
x = x + i
In [3]:
# -- Select number of function calls
j_max = 5
# -- Pre-allocation
t_best = np.zeros(j_max)
t_jit_best = np.zeros(j_max)
iterations = np.zeros(j_max)
for j in range(j_max):
# -- Call testloops and record time
t = %timeit -o -q testloop(10**j)
t_jit = %timeit -o -q testloop_jit(10**j)
# -- Save execution time
t_best[j] = t.best
t_jit_best[j] = t_jit.best
# -- Save iterations
iterations[j] = 10**j
In [4]:
# -- Plot figure
plt.figure(figsize=[10,6])
plt.loglog(iterations,t_best,'o-',label='without jit')
plt.loglog(iterations,t_jit_best,'o-',label='with jit')
plt.legend(loc=2)
plt.grid(True)
plt.ylabel('Execution time (s)')
plt.xlabel('Iterations (-)')
plt.show()
In [ ]: